Completed
Push — master ( 40c2b4...09bd06 )
by Justin
01:29
created

Subject.setIdentifiers   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 6
rs 9.4285
1
var Conclusion = require('./Conclusion'),
2
    Identifiers = require('./Identifiers'),
3
    EvidenceReference = require('./EvidenceReference'),
4
    SourceReference = require('./SourceReference'),
5
    utils = require('./utils');
6
7
/**
8
 * An object identified in time and space by various conclusions.
9
 * 
10
 * @constructor
11
 * @param {Object} [json]
0 ignored issues
show
Documentation introduced by
The parameter [json] does not exist. Did you maybe forget to remove this comment?
Loading history...
12
 */
13
var Subject = function(json){
14
  
15
  // Protect against forgetting the new keyword when calling the constructor
16
  if(!(this instanceof Subject)){
17
    return new Subject(json);
18
  }
19
  
20
  // If the given object is already an instance then just return it. DON'T copy it.
21
  if(Subject.isInstance(json)){
22
    return json;
23
  }
24
  
25
  Conclusion.call(this, json);
26
  
27
  if(json){
0 ignored issues
show
Complexity Best Practice introduced by
There is no return statement if json is false. Are you sure this is correct? If so, consider adding return; explicitly.

This check looks for functions where a return statement is found in some execution paths, but not in all.

Consider this little piece of code

function isBig(a) {
    if (a > 5000) {
        return "yes";
    }
}

console.log(isBig(5001)); //returns yes
console.log(isBig(42)); //returns undefined

The function isBig will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly return undefined.

This behaviour may not be what you had intended. In any case, you can add a return undefined to the other execution path to make the return value explicit.

Loading history...
28
    // setExtracted defaults to false but when the property is undefined we
29
    // want it to stay that way
30
    if(typeof json.extracted !== 'undefined'){
31
      this.setExtracted(json.extracted);
32
    }
33
    this.setEvidence(json.evidence);
34
    this.setIdentifiers(json.identifiers);
35
    this.setMedia(json.media);
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
36
  }
37
};
38
39
Subject.prototype = Object.create(Conclusion.prototype);
40
41
Subject._gedxClass = Subject.prototype._gedxClass = 'GedcomX.Subject';
42
43
/**
44
 * Check whether the given object is an instance of this class.
45
 * 
46
 * @param {Object} obj
47
 * @returns {Boolean}
48
 */
49
Subject.isInstance = function(obj){
50
  return utils.isInstance(obj, this._gedxClass);
51
};
52
53
/**
54
 * Is this an extracted conclusion?
55
 * 
56
 * @returns {Boolean} extracted
57
 */
58
Subject.prototype.isExtracted = function(){
59
  // Double exclamations force a boolean no matter what type the property
60
  // currently is. The main reason for this is to force undefiend into false.
61
  return !!this.isExtracted;
62
};
63
64
/**
65
 * Set the extracted property
66
 * 
67
 * @param {Boolean} extracted
68
 * @returns {Subject} This instance.
69
 */
70
Subject.prototype.setExtracted = function(extracted){
71
  // Double exclamations force a boolean
72
  this.extracted = !!extracted;
73
  return this;
74
};
75
76
/**
77
 * Get the evidence.
78
 * 
79
 * @returns {EvidenceReference[]}
80
 */
81
Subject.prototype.getEvidence = function(){
82
  return this.evidence || [];
83
};
84
85
/**
86
 * Set the evidence. This method will replace existing evidence entries with new entries.
87
 * 
88
 * @param {Object[]|EvidenceReference[]}
0 ignored issues
show
Documentation introduced by
The parameter * does not exist. Did you maybe forget to remove this comment?
Loading history...
89
 * @returns {Subject} This instance.
90
 */
91
Subject.prototype.setEvidence = function(evidence){
92
  return this._setArray(evidence, 'evidence', 'addEvidence');
93
};
94
95
/**
96
 * Add evidence
97
 * 
98
 * @param {Object|EvidenceReference}
0 ignored issues
show
Documentation introduced by
The parameter * does not exist. Did you maybe forget to remove this comment?
Loading history...
99
 * @returns {Subject} This instance.
100
 */
101
Subject.prototype.addEvidence = function(evidence){
102
  return this._arrayPush(evidence, 'evidence', EvidenceReference);
103
};
104
105
/**
106
 * Get the identifiers
107
 * 
108
 * @return {Identifiers}
109
 */
110
Subject.prototype.getIdentifiers = function(){
111
  return this.identifiers;
112
};
113
114
/**
115
 * Set the identifiers
116
 * 
117
 * @param {Object|Identifiers}
0 ignored issues
show
Documentation introduced by
The parameter * does not exist. Did you maybe forget to remove this comment?
Loading history...
118
 * @returns {Subject} This instance
119
 */
120
Subject.prototype.setIdentifiers = function(identifiers){
121
  if(identifiers){
122
    this.identifiers = Identifiers(identifiers);
123
  }
124
  return this;
125
};
126
127
/**
128
 * Get the media references
129
 * 
130
 * @returns {SourceReference[]}
131
 */
132
Subject.prototype.getMedia = function(){
133
  return this.media || [];
134
};
135
136
/**
137
 * Set the media references
138
 * 
139
 * @param {Object[]|SourceReference[]}
140
 */
141
Subject.prototype.setMedia = function(media){
142
  return this._setArray(media, 'media', 'addMedia');
143
};
144
145
/**
146
 * Add media
147
 * 
148
 * @param {Object|SourceReference}
0 ignored issues
show
Documentation introduced by
The parameter * does not exist. Did you maybe forget to remove this comment?
Loading history...
149
 * @returns {Subject} This instance.
150
 */
151
Subject.prototype.addMedia = function(media){
152
  return this._arrayPush(media, 'media', SourceReference);
153
};
154
155
/**
156
 * Export the object as JSON
157
 * 
158
 * @return {Object} JSON object
159
 */
160
Subject.prototype.toJSON = function(){
161
  return this._toJSON(Conclusion, [
162
    'extracted',
163
    'evidence',
164
    'identifiers',
165
    'media'
166
  ]);
167
};
168
169
module.exports = Subject;